package com.qdxht.dispenser.base;

import android.os.Bundle;
import android.view.ViewGroup;
import android.view.Window;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.StringRes;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewbinding.ViewBinding;
import com.qdxht.dispenser.R;
import com.qdxht.dispenser.base.action.TitleBarAction;
import com.qdxht.dispenser.base.action.ToastAction;
import com.qdxht.dispenser.ui.activity.main.MainActivity;
import com.qdxht.dispenser.ui.activity.user.LoginActivity;
import com.qdxht.dispenser.base.action.KeyboardAction;
import com.qdxht.dispenser.interfaces.ExitCallback;
import com.qdxht.dispenser.ui.activity.user.RegisterActivity;
import com.qdxht.dispenser.utils.ExitUtil;
import com.qdxht.dispenser.view.loading.LoadingBar;
import com.gyf.immersionbar.ImmersionBar;
import com.hjq.bar.TitleBar;
import com.hjq.base.action.BundleAction;
import com.hjq.base.action.ClickAction;

/**
 * @author GJ
 * @Description: BaseActivity
 * @date 2022/6/2 16:46
 */
public abstract class BaseActivity<T extends ViewBinding>  extends AppCompatActivity
        implements KeyboardAction , TitleBarAction, ClickAction, ToastAction , BundleAction {

    protected T binding;

    public BaseActivity mActivity;

    /** 标题栏对象 */
    private TitleBar mTitleBar;
    /** 状态栏沉浸 */
    private ImmersionBar mImmersionBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if(savedInstanceState != null){
            AppApplication application = (AppApplication)getApplication();
            if(application != null && !application.isLoadLazySdk()){
                application.initLazySdk();
            }
            finish();
            MainActivity.start(this);
            return;
        }
        binding = getBinding();
        setContentView(binding.getRoot());
        mActivity = this;

        initSoftKeyboard();
        initView();
        initData();

        if (getTitleBar() != null) {
            getTitleBar().setOnTitleBarListener(this);
        }

        // 初始化沉浸式状态栏
        if (isStatusBarEnabled()) {
            getStatusBarConfig().init();

            // 设置标题栏沉浸
            if (getTitleBar() != null) {
                ImmersionBar.setTitleBar(this, getTitleBar());
            }
        }
    }

    protected abstract T getBinding();

    /**
     * 初始化控件
     */
    protected abstract void initView();

    /**
     * 初始化数据
     */
    protected abstract void initData();

    /**
     * 和 setContentView 对应的方法
     */
    public ViewGroup getContentView() {
        return findViewById(Window.ID_ANDROID_CONTENT);
    }

    /**
     * 初始化软键盘
     */
    protected void initSoftKeyboard() {
        // 点击外部隐藏软键盘，提升用户体验
        getContentView().setOnClickListener(v -> {
            // 隐藏软键，避免内存泄漏
            hideKeyboard(getCurrentFocus());
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        LoadingBar.getInstance().cancel();
    }

    @Override
    public void finish() {
        super.finish();
        // 隐藏软键，避免内存泄漏
        hideKeyboard(getCurrentFocus());
    }

    @Override
    public void onBackPressed() {
        if (mActivity instanceof MainActivity || mActivity instanceof LoginActivity || mActivity instanceof RegisterActivity) {
            ExitUtil.getInstance().exit(this, new ExitCallback() {
                @Override
                public void callback() {
                }
            });
        } else {
//            ActivityManager.getInstance().popActivity(mActivity);
            finish();
        }
    }

    /**
     * 是否使用沉浸式状态栏
     */
    protected boolean isStatusBarEnabled() {
        return true;
    }

    /**
     * 状态栏字体深色模式
     */
    protected boolean isStatusBarDarkFont() {
        return true;
    }

    /**
     * 获取状态栏沉浸的配置对象
     */
    @NonNull
    public ImmersionBar getStatusBarConfig() {
        if (mImmersionBar == null) {
            mImmersionBar = createStatusBarConfig();
        }
        return mImmersionBar;
    }

    /**
     * 初始化沉浸式状态栏
     */
    @NonNull
    protected ImmersionBar createStatusBarConfig() {
        return ImmersionBar.with(this)
                // 默认状态栏字体颜色为黑色
                .statusBarDarkFont(isStatusBarDarkFont())
                // 指定导航栏背景颜色
                .navigationBarColor(R.color.white)
                // 状态栏字体和导航栏内容自动变色，必须指定状态栏颜色和导航栏颜色才可以自动变色
                .autoDarkModeEnable(true, 0.2f);
    }

    /**
     * 设置标题栏的标题
     */
    @Override
    public void setTitle(@StringRes int id) {
        setTitle(getString(id));
    }

    /**
     * 设置标题栏的标题
     */
    @Override
    public void setTitle(CharSequence title) {
        super.setTitle(title);
        if (getTitleBar() != null) {
            getTitleBar().setTitle(title);
        }
    }

    @Override
    @Nullable
    public TitleBar getTitleBar() {
        if (mTitleBar == null) {
            mTitleBar = obtainTitleBar(getContentView());
        }
        return mTitleBar;
    }

    @Override
    public void onLeftClick(TitleBar titleBar) {
        onBackPressed();
    }

    @Override
    public Bundle getBundle() {
        return getIntent().getExtras();
    }

}
